package com.yxplearn.mybatisplus.util.asyn;

import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.*;

@Component
public class AsynManager implements Runnable {

    private LinkedBlockingDeque<DbTask> linkedBlockingDeque = new LinkedBlockingDeque<DbTask>();

    private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    public void addTask(DbTask dbTask){
        System.out.println("addTime" + new Date());
        linkedBlockingDeque.add(dbTask);
    }

    public AsynManager(){
        executorService.scheduleWithFixedDelay(this, 1000, 10000, TimeUnit.MILLISECONDS);
    }

    @Override
    public void run() {
//        System.out.println("3" + new Date());
        DbTask dbTask = linkedBlockingDeque.poll();
        if (dbTask != null)
            executorService.submit(dbTask);
    }
}
